<?php

function canEdit(&$oper, &$user = null)
{
	return true;
}

function canModify(&$oper, &$user = null)
{
	if(!isBuy($oper) || $oper['ostatok'] <= 0)
	{
		return false;
	}
	static $root_id;
	static $me;
	$CI = &get_instance();
	if(!$root_id)
	{
		$root = $CI->MUser->getRoot();
		$root_id = $root['id'];
	}
	if(!$me)
	{
		$me = $CI->MUser->get(Auth::$user_id);
	}
	if(Auth::$user_id == $root_id) return true;
	if(empty($user))
	{
		$user = $CI->MUser->get($oper['user_id']);
	}
	return 
		$user['parent_id'] == Auth::$user_id ||
		($user['id'] == Auth::$user_id && (substr($oper['type'], 0, 2) == 'l*' || $oper['type'] == 'ref' || $me['can_oper']))
	;
}

function canSell(&$oper, &$user = null)
{
	if(!(isBuy($oper) && ($oper['type'] == 'week' || $oper['ostatok'] > 0)))
	{
		return false;
	}
	static $root_id;
	static $me;
	$CI = &get_instance();
	if(!$root_id)
	{
		$root = $CI->MUser->getRoot();
		$root_id = $root['id'];
	}
	if(!$me)
	{
		$me = $CI->MUser->get(Auth::$user_id);
	}
	if($oper['user_id'] == Auth::$user_id && substr($oper['type'], 0, 2) == 'l*') return false;
	if(Auth::$user_id == $root_id) return true;
	if(empty($user))
	{
		$user = $CI->MUser->get($oper['user_id']);
	}
	if($me['id'] == $user['id'] && $me['can_oper']) return true;
	return $user['parent_id'] == Auth::$user_id;
}

function canDelete(&$oper, &$user = null)
{
	static $root_id;
	$CI = &get_instance();
	if(!$root_id)
	{
		$root = $CI->MUser->getRoot();
		$root_id = $root['id'];
	}
	if(Auth::$user_id == $root_id || Auth::isSupport()) return true;
	static $date_can_delete;
	static $me;
	if(!$date_can_delete)
	{
		$date_can_delete = date("Y-m-d", strtotime('-1 month'));
	}
	if(!$me)
	{
		$me = $CI->MUser->get(Auth::$user_id);
	}
	if(empty($user))
	{
		$user = $CI->MUser->get($oper['user_id']);
	}
	return 
		(
			$user['parent_id'] == Auth::$user_id || 
			($user['id'] == Auth::$user_id &&  ((!in_array($oper['type'], array('sell', 'sell.mass')) && substr($oper['type'], 0, 9) != 'sell.fire') || $me['can_oper']))
		) && 
		($oper['type'] != 'buy' || $oper['date'] > $date_can_delete || $me['can_oper_delete'])
		&&  (!isBuy($oper) || $oper['type'] == 'week' || $oper['ostatok'] > 0)
		;
}

function canRecountDes(&$oper, &$user = null)
{
	static $date_can_delete;
	static $me;
	$CI = &get_instance();
	if(!$date_can_delete)
	{
		$date_can_delete = date("Y-m-d", strtotime('-1 month'));
	}
	if(!$me)
	{
		$me = $CI->MUser->get(Auth::$user_id);
	}
	return $oper['type'] == 'buy' && ($oper['date'] > $date_can_delete  || $me['can_oper_delete']);
}

function canRecountFrozen($oper)
{
	return Auth::isSupport() && $oper['ostatok'] > 0 && isBuy($oper);
}







function isBuy(&$oper)
{
	global $TYPE_OPER_SELL;
	return !in_array($oper['type'], $TYPE_OPER_SELL);
}

function getDepoMonth($lgota, $percent)
{
	if ($lgota && ($percent == 40 || $percent == 55)) return 3; 
	if ($lgota && ($percent == 50 || $percent == 65)) return 6; 
	if ($lgota && ($percent == 60 || $percent == 75)) return 12; 
	if (!$lgota && ($percent == 30 || $percent == 45)) return 3; 
	if (!$lgota && ($percent == 40 || $percent == 55)) return 6; 
	if (!$lgota && ($percent == 50 || $percent == 65)) return 12; 
	return '';
}

function getPercentByType($type, $sverh, &$user, $date = null)
{
	$CI = @get_instance();
	$percent = $type == 'dep12' ? 50 : ($type == 'dep6' ? 40 : ($type == 'dep3' ? 30 : ($type == 'week' ? 10 : 20)));
	if($type != 'week' && $CI->MUser->isLgota($user, $date))
	{
		$percent += 10;
	}
	if($type != 'week' && $type != 'base' && $sverh)
	{
		$percent += 15;
	}
	return $percent;
}

function operUpdateFrozen(&$oper)
{
	$CI = &get_instance();
	$str_frozen_date = null;
	$frozen_date = null;
	switch(@$oper['type'])
	{
		case "reg":
			if(@$oper['depo']) $str_frozen_date = '+'.intval($oper['depo'])." months, +1 day";
			else $str_frozen_date = '+2 weeks, +1 day';
			break;

		case "week":
			$str_frozen_date = '+1 week, +1 day';
			$frozen_summ = $oper['summ'] * 1.1;
			break;

		case "buy":
		case "ng":
		case "patriot":
		case "arest":
		case "8marta":
			if(@$oper['depo']) $str_frozen_date = '+'.intval($oper['depo'])." months, +1 day";
			else $str_frozen_date = '+1 month, +1 day';
			break;

		case "ref":
		case "l*":
		case "l**":
		case "l***":
		case "l****":
		case "l*****":
		case "l******":
		case "l*******":
		case "l********":
			$str_frozen_date = '+1 month, +1 day';
			break;

		case "modify.buy":
			$parent_oper_sale = $CI->MOper->get($oper['parent_id']);
			$str_frozen_date = @$oper['depo'] ? '+'.intval($oper['depo'])." months, +1 day" : '';
			if(@$parent_oper_sale['id'])
			{
				$parent_oper = $CI->MOper->get($parent_oper_sale['parent_id']);
				if(@$parent_oper['id'] && $parent_oper['depo'] == @$oper['depo'] && abs($parent_oper['percent'] - @$oper['percent']) < 15)
				{
					$frozen_date = @$parent_oper['frozen_date'];
				}
			}
			break;
	}
	if($str_frozen_date || $frozen_date)
	{
		$time = strtotime($oper['date']);
		if(!$frozen_date)
		{
			$frozen_date = date("Y-m-d", strtotime($str_frozen_date, $time));
		}
		if(!@$frozen_summ)
		{
			$kurs = $CI->MKurs->getKurs($frozen_date, $oper['percent'], 'sell');
			$frozen_summ = round($kurs * $oper['ostatok'], 2);
		}
		$save = array(
			'frozen_date' => $frozen_date,
			'frozen_summ' => $frozen_summ,
		);
		$CI->MOper->save($save, $oper['id']);
	}
}
